package base;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;

/**
 * mp3 信息
 * Created by sk.z on 2018/10/27.
 */
public class MP3Info {

    private String charset = "gbk"; //解析MP3信息时用的字符编码
    private byte[] buf; //MP3的标签信息的byte数组

    public MP3Info(File mp3) throws IOException {
        buf = new byte[128]; //初始化标签信息的byte数组

        RandomAccessFile raf = new RandomAccessFile(mp3, "r"); //随机读写方式打开MP3文件
        raf.seek(raf.length() - 128);// 移动到文件MP3末尾
        raf.read(buf);// 读取标签信息
        raf.close();// 关闭文件
        if (buf.length != 128) {// 数据是否合法
            throw new IOException("MP3标签信息数据长度不合法!");
        }
        if (!"TAG".equalsIgnoreCase(new String(buf, 0, 3))) {// 信息格式是否正确
            throw new IOException("MP3标签信息数据格式不正确!");
        }
    }


    /**
     * 获得目前解析时用的字符编码
     * @return 目前解析时用的字符编码
     */
    public String getCharset() {
        return charset;
    }

    /**
     * 设置解析时用的字符编码
     * @param charset 解析时用的字符编码
     */
    public void setCharset(String charset) {
        this.charset = charset;
    }

    public String getSongName() {
        try {
            return new String(buf, 3, 30, charset).trim();
        } catch (UnsupportedEncodingException e) {
            return new String(buf, 3, 30).trim();
        }
    }

    public String getArtist() {
        try {
            return new String(buf, 33, 30, charset).trim();
        } catch (UnsupportedEncodingException e) {
            return new String(buf, 33, 30).trim();
        }
    }

    public String getAlbum() {
        try {
            return new String(buf, 63, 30, charset).trim();
        } catch (UnsupportedEncodingException e) {
            return new String(buf, 63, 30).trim();
        }
    }

    public String getYear() {
        try {
            return new String(buf, 93, 4, charset).trim();
        } catch (UnsupportedEncodingException e) {
            return new String(buf, 93, 4).trim();
        }
    }

    public String getComment() {
        try {
            return new String(buf, 97, 28, charset).trim();
        } catch (UnsupportedEncodingException e) {
            return new String(buf, 97, 28).trim();
        }
    }


}